import {
	payOrderByBalance,
	payVipPurchaseByBalance,
	payBoxByBalance,
	payOrderBySpend,
	payOrderByGoldCoin
} from "api/paymentApi";

export async function balancepay(id, type = 'order') {
	/**
	 * id: order_id, vip_purchase_id
	 */
	if (type === 'order') {
		await payOrderByBalance(id);
		uni.showModal({
			title: '提示',
			content: '订单支付成功',
			showCancel: false,
			success: async (res) => {
				uni.redirectTo({
					url: `/packs/pages/order/paidBySelf`
				})
			}
		})
	}
	if (type === 'vipPurchase') {
		await payVipPurchaseByBalance(id)
		uni.showModal({
			title: '提示',
			content: 'VIP会员付费成功',
			showCancel: false,
			success: async (res) => {
				uni.switchTab({
					url: `/pages/mine/mine`
				})
			}
		})
	}
	if (type === 'box') {
		await payBoxByBalance(id)
		uni.showModal({
			title: '提示',
			content: '盲箱购买成功',
			showCancel: false,
			success: async (res) => {
				
				uni.redirectTo({
					url: `/packs/pages/box/refresh?order_id=`+ id
				})
			}
		})
	}
}

export async function spendpay(id, type = 'order') {
	/**
	 * id: order_id
	 */
	if (type === 'order') {
		await payOrderBySpend(id)
		uni.showModal({
			title: '提示',
			content: '订单支付成功',
			showCancel: false,
			success: async (res) => {
				uni.redirectTo({
					url: `/packs/pages/order/paidBySelf`
				})
			}
		})
	}

}

export async function goldCoinPay(id, type = 'order') {
	/**
	 * id: order_id
	 */
	if (type === 'order') {
		let response = await payOrderByGoldCoin(id)
		
		if (!response.message) {
			uni.showModal({
				title: '提示',
				content: '订单支付成功',
				showCancel: false,
				success: async (res) => {
					uni.redirectTo({
						url: `/packs/pages/order/paidBySelf`
					})
				}
			})
		} else {
			uni.showModal({
				title: '提示',
				content: response.errors.err[0],
				showCancel: false
			})
		}

	}
	if (type === 'againorder') {
		let response = await payOrderByGoldCoin(id)
		if (!response.message) {
			uni.showModal({
				title: '提示',
				content: '订单支付成功',
				showCancel: false,
				success: async (res) => {
					uni.navigateBack({})
				}
			})
		} else {
			uni.showModal({
				title: '提示',
				content: response.errors.err[0],
				showCancel: false
			})
		}

	}
}

export async function wxpay(payload, type = 'order') {
	/**
	 * type: order, vipPurchase
	 */
	uni.requestPayment({
		provider: 'wxpay',
		timeStamp: payload.timeStamp,
		nonceStr: payload.nonceStr,
		package: payload.package,
		signType: payload.signType,
		paySign: payload.paySign,
		success: function(res) {
			if (type === 'order') {
				uni.showModal({
					title: '提示',
					content: '订单支付成功',
					showCancel: false,
					success: async (res) => {
						uni.redirectTo({
							url: `/packs/one/pages/order/paid`
						})
					}
				})
			}
			if (type === 'vipPurchase') {
				uni.showModal({
					title: '提示',
					content: 'VIP会员付费成功',
					showCancel: false,
					success: async (res) => {
						uni.switchTab({
							url: `/pages/mine/mine`
						})
					}
				})
			}
		},
		fail: function(err) {
			uni.showModal({
				title: '提示',
				content: '支付失败！',
				showCancel: false
			})
		}
	})
}
